| Conditions | 1 |
| Paths | 1 |
| Total Lines | 118 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 2 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import expect from 'expect'; |
||
| 5 | describe('State Getter Function', () => { |
||
| 6 | |||
| 7 | // sample redux wrapper for state.get |
||
| 8 | function getState(...props) { |
||
| 9 | return true; |
||
| 10 | } |
||
| 11 | |||
| 12 | function getStateWithImmutable(...props) { |
||
| 13 | return fromJS({ |
||
| 14 | x: 1 |
||
| 15 | }); |
||
| 16 | } |
||
| 17 | |||
| 18 | it('Should return state if its registered', () => { |
||
| 19 | const state = { filterState: { get: getState } }; |
||
| 20 | const props = {}; |
||
| 21 | expect( |
||
| 22 | stateGetter(state, props, 'filterState', 'someProp') |
||
| 23 | ).toBeTruthy(); |
||
| 24 | }); |
||
| 25 | |||
| 26 | it(['Should return plain object if state is stored as a immutable', |
||
| 27 | ' and using a dynamic reducerKey'].join(''), () => { |
||
| 28 | const state = { someFilterState: { get: getStateWithImmutable } }; |
||
| 29 | const props = { |
||
| 30 | reducerKeys: { |
||
| 31 | filterState: 'someFilterState' |
||
| 32 | } |
||
| 33 | }; |
||
| 34 | |||
| 35 | expect( |
||
| 36 | stateGetter(state, props, 'someFilterState', 'someProp') |
||
| 37 | ).toEqual({ |
||
| 38 | x: 1 |
||
| 39 | }); |
||
| 40 | }); |
||
| 41 | |||
| 42 | it('Should return plain object if state is stored as a immutable', () => { |
||
| 43 | const state = { filterState: { get: getStateWithImmutable } }; |
||
| 44 | const props = {}; |
||
| 45 | expect( |
||
| 46 | stateGetter(state, props, 'filterState', 'someProp') |
||
| 47 | ).toEqual({ |
||
| 48 | x: 1 |
||
| 49 | }); |
||
| 50 | }); |
||
| 51 | |||
| 52 | it('Should return state even if the casing is off', () => { |
||
| 53 | const state = { filterState: { get: getState } }; |
||
| 54 | const props = {}; |
||
| 55 | |||
| 56 | expect(stateGetter( |
||
| 57 | state, props, 'filterstate', 'someProp') |
||
| 58 | ).toBeTruthy(); |
||
| 59 | expect(stateGetter( |
||
| 60 | state, props, 'FILTERSTATE', 'someProp') |
||
| 61 | ).toBeTruthy(); |
||
| 62 | expect(stateGetter( |
||
| 63 | state, props, 'FilterState', 'someProp') |
||
| 64 | ).toBeTruthy(); |
||
| 65 | }); |
||
| 66 | |||
| 67 | it('Should return null if it\'s not registered', () => { |
||
| 68 | const state = { filterState: { get: getState } }; |
||
| 69 | const props = {}; |
||
| 70 | expect( |
||
| 71 | stateGetter(state, props, 'unknownState', 'someProp') |
||
| 72 | ).toEqual(null); |
||
| 73 | }); |
||
| 74 | |||
| 75 | it('Should return state when a dynamic key is used if registerd', () => { |
||
| 76 | const state = { someFilterState: { get: getState } }; |
||
| 77 | const props = { |
||
| 78 | reducerKeys: { |
||
| 79 | filterState: 'someFilterState' |
||
| 80 | } |
||
| 81 | }; |
||
| 82 | expect( |
||
| 83 | stateGetter(state, props, 'filterState', 'someProp') |
||
| 84 | ).toBeTruthy(); |
||
| 85 | }); |
||
| 86 | |||
| 87 | it(['Should return state when a dynamic key ', |
||
| 88 | 'is used, and has immutable state'].join(''), () => { |
||
| 89 | const state = { someFilterState: { get: getStateWithImmutable } }; |
||
| 90 | const props = { |
||
| 91 | reducerKeys: { |
||
| 92 | filterState: 'someFilterState' |
||
| 93 | } |
||
| 94 | }; |
||
| 95 | expect( |
||
| 96 | stateGetter(state, props, 'filterState', 'someProp') |
||
| 97 | ).toEqual({ |
||
| 98 | x: 1 |
||
| 99 | }); |
||
| 100 | }); |
||
| 101 | |||
| 102 | it('Should return null if a dynamic key is used if not registered', () => { |
||
| 103 | const state = {}; |
||
| 104 | const props = { |
||
| 105 | reducerKeys: { |
||
| 106 | filterState: 'someFilterState' |
||
| 107 | } |
||
| 108 | }; |
||
| 109 | expect( |
||
| 110 | stateGetter(state, props, 'filterState', 'someProp') |
||
| 111 | ).toEqual(null); |
||
| 112 | }); |
||
| 113 | |||
| 114 | it('Should return null when no keys and no state are provided', () => { |
||
| 115 | const state = {}; |
||
| 116 | const props = {}; |
||
| 117 | expect( |
||
| 118 | stateGetter(state, props, 'filterState', 'someProp') |
||
| 119 | ).toEqual(null); |
||
| 120 | }); |
||
| 121 | |||
| 122 | }); |